package code.rocky.consumer;

import java.util.concurrent.TimeUnit;

public class ConsumerDemo02 {

    private volatile static int i = 0;

    public static void main(String[] args) {
        Thread p = new Thread(new Producer(),"producer");
        Thread c = new Thread(new Consumer(),"consumer");
        p.start();
        c.start();

        try {
            p.join();
            c.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

     static class Producer implements Runnable {

        @Override
        public void run() {
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i++;
                System.out.println(Thread.currentThread().getName() + " i的值为:" + i);
                if (i >= 3) {
                    System.out.println(Thread.currentThread().getName() + " break时 i的值为:" + i);
                    break;
                }
            }
        }
    }

    static class Consumer implements Runnable {

        @Override
        public void run() {
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (i == 3) {
                    System.out.println(Thread.currentThread().getName() +"  正在消费了");
                    break;
                }
                else {
                    System.out.println(Thread.currentThread().getName() +"  咋还不消费了");
                }
            }
        }
    }
}
